<?php

//Début de la session
session_start();

//Librairie de fonctions
require (__DIR__ . '/vendor/autoload.php');
require_once("lib/libconfig.inc.php");
require_once("lib/libsession.inc.php");
require_once("lib/libfonctions.inc.php");
require_once("lib/libmysql.inc.php");
require_once("lib/libencode.inc.php");
require_once("lib/libforms.inc.php");
require_once("lib/libcourriel.inc.php");
require_once("lib/libliberations.inc.php");
require_once("lib/lib2fa.inc.php");

//Encodage des caractères
ini_set('display_errors', 'On');
header('Content-Type: text/html; charset=$ENCODAGE');

setConnectionSql();

$search = antiInjection(sprintf("%d", filter_input(INPUT_POST, 'prm1')));
$search2 = antiInjection(filter_input(INPUT_POST, 'prm2'));
$jeton = antiInjection(filter_input(INPUT_POST, 'jeton'));

if (isset($search) && !empty($search)) {
    if ($search != "") {
        unset($sqlparam);

        unset($dir2);

        switch ($search2) {
            case "asc":
            case "desc":
                $dir2 = $search2;
                break;
            default:
                $dir2 = "asc";
                break;
        }

        $tag = $search;
        unset($sqlparam);
        $sqlparam["table"][] = "periodes";
        $sqlparam["table"][] = "nombreemployes";
        $sqlparam["join"][] = "nombreemployes.refperiode=periodes.refperiode"; //] = "left";
        $sqlparam["champs"][] = "YEAR(periodes.periode)-1 as periode";
        $sqlparam["champs"][] = "nombreemployes.nombreemployes";
        $sqlparam["champs"][] = "nombreemployes.refemployeur";
        $sqlparam["champs"][] = "periodes.refperiode";
        $sqlparam["where"][] = sprintf("nombreemployes.refemployeur=%d", $tag);
        $sqlparam["ordre"] = "periodes.periode $dir2";

        $result = executerRequeteSql($sqlparam);

        $i = 1;
        if ($result) {
            unset($dir3);
            $dir3 = $dir2;
            $dir2 = ($dir2 == "asc") ? "desc" : "asc";
            print "<table style='border-collapse:collapse; width:50%; margin:auto;'>";
            print "<tr style='height:35px;'>";
            $style = "font-family:arial, helvetica, sans-serif; font-size:9pt; background-color:#00aadb;";
            print "<th style='$style'>#</th>";
            print "<th style='$style'><a class='tri' style='cursor:pointer; color:#0000ee;' onclick='requestNombre(\"$tag\",\"$dir2\");'>P&eacute;riode";
            print "&nbsp;<IMG SRC='images/s_$dir2.png' style='border:0px;'></a></th>\n";
            print "<th style='$style'>Nombre</th>";
            print "<th style='$style'>Action</th>";
            print "</tr>";
            $style = "border:1px solid #cfcfcf; font-family:arial, helvetica, sans-serif;; color:black; font-size:9pt; margin-left:5px; margin-right:5px; text-align:center;";

            foreach ($result as $clef => $valeur2) {
                $valeur0 = htmlentities($valeur2["periode"], ENT_QUOTES, $ENCODAGE) . "-04-01";
                $valeur1 = htmlentities($valeur2["nombreemployes"], ENT_QUOTES, $ENCODAGE);
                
                print "<tr>\n";
                print "<td style='$style'>$i</td>\n";
                print "<td style='$style'>$valeur0</td>\n";
                print "<td style='$style'>\n";
                print "<form id='changement$i' name='changement$i' method=\"post\" action=\"index2.php?page=liste_nombre_employes\">\n";
                print "<input type='hidden' name='lperiode' id='lperiode' value='" . $valeur2["refperiode"] . "'>\n";
                print "<input type='hidden' name='lemployeur' id='lemployeur' value='$tag'>\n";
                print "<input type='hidden' name='action' id='action' value='Modifier'>\n";
                print "<input type='text' name='lvaleur' id='lvaleur' value='$valeur1'>\n";
                print "<input type='hidden' name='jeton' id='jeton' value='$jeton'>\n";
                print "</form>";
                print "</td>\n";
                print "<td style='$style'>\n";
                print "<input class='modifier' title='Modifier' TYPE='submit' name='action' VALUE='Modifier' onclick='document.changement$i.submit();'>\n"; //placervaleur(\"" . $valeur2["refperiode"] . "\",\"lnombre" . $tag . "_$i\");'>\n";
                print "</td></tr>\n";
                $i++;
            }
            print "</table>";
        }
    }
}